<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="传感器编号">
              <a-input v-model="queryParam.sensorId" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.AddInfo.add()">新建</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-info ref="RemoveInfo" :infoIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <span slot="info" slot-scope="text, record">
        <template>
          <a @click="getInfo(record)">详情</a>
          <a-divider type="vertical" />
          <a @click="getSession(record)">历史记录</a>
        </template>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <remove-info ref="RemoveInfo" :infoIds="String(record.infoId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <query-info ref="QueryInfo" @ok="handleOk" ></query-info>
    <add-info ref="AddInfo" @ok="handleOk"/>
    <edit-info ref="EditInfo" @ok="handleOk"/>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import { queryInfo } from '@/api/medical/info'
import AddInfo from './modules/info/AddInfo'
import EditInfo from './modules/info/EditInfo'
import RemoveInfo from './modules/info/RemoveInfo'
import QueryInfo from '@/views/medical/modules/info/QueryInfo'
// 用于处理过滤器动态获取数据
let that // eslint-disable-line no-unused-vars

export default {
  name: 'InfoList',
  components: {
    QueryInfo,
    STable,
    AddInfo,
    EditInfo,
    RemoveInfo
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '心电图编号',
          dataIndex: 'infoId',
          align: 'center',
          sorter: true
        },
        {
          title: '传感器编号',
          dataIndex: 'sensorId',
          align: 'center',
          sorter: true
        },
        {
          title: 'ecg',
          dataIndex: 'infoImage',
          align: 'center',
          scopedSlots: { customRender: 'info' }
        },
        {
          title: '操作',
          ataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return queryInfo(Object.assign(parameter, queryJson))
          .then(res => {
            return res.data.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {

  },
  created () {
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleEdit (record) {
      this.$refs.EditInfo.edit(record)
    },
    getInfo (record) {
      this.$refs.QueryInfo.getInfo(record)
    },
    getSession (record) {
      this.$router.replace({ name: 'medicalSession',
        params: { sensorId: record.sensorId } })
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
